#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import random
from io import StringIO
from django.core.paginator import Paginator


def vaild_random(count):
    return tuple([random.randint(0, 233) for i in range(count)])


def get_vaild_num(count):
    f = StringIO()
    for i in range(count):
        ran_num = random.choice([random.randint(65, 90), random.randint(97, 122), random.randint(0, 9)])
        get_num = chr(ran_num) if ran_num > 9 else ran_num
        f.write(str(get_num))
    return f.getvalue()


def paginators(object, page):
    paginator = Paginator(object, 5)
    # 用django自带的检验处理
    try:
        page = paginator.validate_number(page)
    except Exception:
        page = 1

    # 这里获取的是文章
    object = paginator.page(page)

    # 这里处理下边页码数
    count_paginator = paginator.num_pages
    """
        - 判断是否大于11，如果小于则有多少返回多少
        - 判断 获取页码-5是否小于等于1， 如果是则返回 1到11页
        - 判断 获取页码+5是否大于总页面， 如果是 则总的页面左边减10，右边加1直到最后一行
        - 其它情况则 获取的页码 左右各减5
    """
    if count_paginator > 11:
        if page - 5 <= 1:
            per_list = range(1, 11)
        elif page + 5 > count_paginator:
            per_list = range(count_paginator - 10, count_paginator + 1)
        else:
            per_list = range(page - 5, page + 5)
    else:
        per_list = paginator.page_range
    return object, per_list, page
